package com.screwdriver.jso.processor;

import com.sun.mirror.declaration.InterfaceDeclaration;
import com.sun.mirror.declaration.MethodDeclaration;
import com.sun.mirror.type.InterfaceType;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

@SuppressWarnings("deprecation")
public class TypeVisitManager {

	private final InterfaceDeclaration root;

	public TypeVisitManager(InterfaceDeclaration root) {
		this.root = root;
	}

	public void traverseMethods(InterfaceVisitor visitor, InterfaceType declaringSuperInterface,
			Collection<? extends MethodDeclaration> methods) {

		visitor.visitSuperInterface(declaringSuperInterface);
		for (MethodDeclaration md : methods) {
			visitor.visitMethod(declaringSuperInterface, md);
		}
	}

	public void traverseTypeHierachy(InterfaceVisitor visitor) {
		visitor.visitRoot(root);
		traverseMethods(visitor, null, root.getMethods());

		List<InterfaceType> toBeProcessed = new LinkedList<InterfaceType>(root.getSuperinterfaces());
		while (toBeProcessed.size() > 0) {
			InterfaceType processedInterface = toBeProcessed.remove(0);

			toBeProcessed.addAll(processedInterface.getSuperinterfaces());

			traverseMethods(visitor, processedInterface, processedInterface.getDeclaration().getMethods());
		}
	}
}